package com.shushan.setting;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetStrCommListener;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.versionupdate.utils.MD5Util;

/**
 * @ClassName: ChildSexSetActivity
 * @Desciption: 更改性别
 * @author: yichaohua
 * @date: 2018-08-13
 */
public class ChildSexSetActivity extends NavigationLoseActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private static final String TAG = "ChildSexSetActivity";

    private RadioGroup sexRadioGroup;
    private RadioButton boyRadioButton;
    private RadioButton girlRadioButton;
    private Button confirmButton;

    private int sex = 1;
    private BabyApplication babyApplication = BabyApplication.getInstance();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.child_sex_set_activity);

        sex = getIntent().getIntExtra("sex",1);

        setTitle("更改性别");

        initView();

    }

    private void initView() {
        sexRadioGroup = findViewById(R.id.child_sex_set_sex_rg);
        sexRadioGroup.setOnCheckedChangeListener(this);
        boyRadioButton = findViewById(R.id.child_sex_set_boy_rb);
        girlRadioButton = findViewById(R.id.child_sex_set_girl_rb);
        confirmButton = findViewById(R.id.child_sex_set_confirm_btn);
        confirmButton.setOnClickListener(this);

        if(sex == 1){
            boyRadioButton.setChecked(true);
        }else {
            girlRadioButton.setChecked(true);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.child_sex_set_boy_rb:
                sex = 1;
                break;
            case R.id.child_sex_set_girl_rb:
                sex = 2;
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.child_sex_set_confirm_btn:
                postSex();
                break;
            default:
                break;
        }
    }

    private void postSex() {
        showWaitDialog();

        int childId = babyApplication.getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.POST_CHILD_SEX);
        urlParse.putValue("child_id", childId)
                .putValue("sex", sex)
                .putValue("sign", MD5Util.MD5LOW(childId + "sszx" + sex));

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetStrCommListener() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(String data) {
                        showToast("性别修改成功");

                        Intent intent = new Intent();
                        intent.putExtra("sex",sex==1?"男":"女");
                        onBackAction(300,intent);
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNetError(netRetBean);
                    }
                });
    }
}
